//
// Created by Major_ on 2017/6/26.
// 描述：js模块封装，数据处理层
// 

/**
 *
 * 公共类
 */
var Util = {
	//get
	get: function(url, data, callback) {
			$.ajax({
				url: url,
				type: "get",
				data: data,
				dataType: "json",
				beforeSend: function() {
					$('#loader').show();
				},
				success: function(res) {
					$('#loader').hide();
					callback(res);
				},
				error: function() {  
				}
			})
	},
	//post
	post: function(url, data, callback) {
			data = JSON.stringify(data),
			$.ajax({
				url: url,
				data: data,
				type: 'post',
				dataType: 'json',
				contentType: "application/json",
				beforeSend: function() {
					$('#loader').show();
				},
				success: function(res) {
					$('#loader').hide();
					console.log(res);
					callback(res);
				},
				error: function() {
				}
			});
	},
	//获取url参数
	GetQueryString: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if(r != null)
			return unescape(r[2]);
		return null;
	},

	//时间戳
	UnixToDate: function(unixTime, isFull, timeZone) {
		if(typeof(timeZone) == 'number') {
			unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
		}
		var time = new Date(unixTime * 1000);
		var ymdhis = "";
		ymdhis += time.getUTCFullYear() + "-";
		ymdhis += (time.getUTCMonth() + 1) + "-";
		ymdhis += time.getUTCDate();
		if(isFull === true) {
			ymdhis += " " + time.getUTCHours() + ":";
			ymdhis += time.getUTCMinutes() + ":";
			ymdhis += time.getUTCSeconds();
		}
		return ymdhis;
	},
	// 跳转
	jump: function(url) {
		window.location.href = url;
	},
	// 分页
	Pinterest: function(callback) {
		var hasmore = true
		$(window).scroll(function() {
			var down = $(document).height() - $(document).scrollTop() - $(window).height();
			if(down <= 50 && hasmore == true) {
				hasmore = false;
				if(callback)
					callback();

			}
			if(down > 50) {
				hasmore = true
			}
		})
	}
}

/**
 *
 * 功能模块
 */
var Cardlist = {
	getCardlist(data, callback) { // 获取信用卡列表
			 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					for(var i in res.data){
						switch(res.data[i].bankName){
							case '邮政储蓄银行': res.data[i].bankbg = 'cardbg-yz';res.data[i].banktop = 'cardtop-yz';break;
							case '招商银行': res.data[i].bankbg = 'cardbg-zs';res.data[i].banktop = 'cardtop-zs';break;
							case '建设银行': res.data[i].bankbg = 'cardbg-js';res.data[i].banktop = 'cardtop-js';break;
							case '工商银行': res.data[i].bankbg = 'cardbg-gs';res.data[i].banktop = 'cardtop-gs';break;
							case '光大银行': res.data[i].bankbg = 'cardbg-gd';res.data[i].banktop = 'cardtop-gd';break;
							case '浦发银行': res.data[i].bankbg = 'cardbg-pf';res.data[i].banktop = 'cardtop-pf';break;
							case '平安银行': res.data[i].bankbg = 'cardbg-pa';res.data[i].banktop = 'cardtop-pa';break;
							case '民生银行': res.data[i].bankbg = 'cardbg-ms';res.data[i].banktop = 'cardtop-ms';break;
							default: res.data[i].bankbg = 'cardbg-default';res.data[i].banktop = 'cardtop-default';break;
						}
					}
					html = template.render("teamlate_bankCardbox", res);
					$('#bankCardbox').html(html);
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});
		},
	setOrderConf(data, callback) { //设置账单日、还款日
		 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					$('.setorderdbox').hide();
					layer.open({content: res.message ,skin: 'msg',time: 2});
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});
	},
	generationPlan(data, callback){ //生成计划预览
			 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					res.data.DateText = sessionStorage.DateText;
					res.data.capitalAmount = (res.data.capitalAmount/100).toFixed(2);
					res.data.totalAmount = (res.data.totalAmount/100).toFixed(2);
					res.data.poundageTotalAmt = (res.data.poundageTotalAmt/100).toFixed(2);
					res.data.firstDay = '';
					for(var i in res.data.planDetails){ //数据处理调整时间格式等
						var repay = 0;
						var consume = 0;
						for(var k in res.data.planDetails[i]){ //当天消费金额与还款金额总额
							if(res.data.firstDay == ''){
								res.data.firstDay = res.data.planDetails[i][k].planDate;
							}
							res.data.planDetails[i][k].planDate = res.data.planDetails[i][k].planDate.split('-')[1]+'-'+res.data.planDetails[i][k].planDate.split('-')[2];
							if(res.data.planDetails[i][k].planType == 20){ //当日还款总额
								repay = repay+res.data.planDetails[i][k].planAmount;
							}
							if(res.data.planDetails[i][k].planType == 10){ //当日消费总额
								consume = consume+res.data.planDetails[i][k].planAmount;
							}
							res.data.planDetails[i][k].planAmount = (res.data.planDetails[i][k].planAmount/100).toFixed(2);
						}
						res.data.planDetails[i][0].repay = (repay/100).toFixed(2);
						res.data.planDetails[i][0].consume = (consume/100).toFixed(2);
					}
					sessionStorage.batchNo = res.data.batchNo; //缓存批次号
					sessionStorage.capitalAmount = res.data.capitalAmount; //缓存保证金
					sessionStorage.poundageTotalAmt = res.data.poundageTotalAmt; //缓存手续费
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});
	},
	planConfirm(data, callback){ //确认计划
		 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});	
	},
	getPayCardList(data, callback){ //获取支付银行卡列表
		 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					for(var i in res.data){
						res.data[i].cardNo = res.data[i].cardNo.split(' ')[3];
					}
					html = template.render("template_bankCardcont", res);
					$("#bankCardcontBox").html(html);
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});
	},
	getUserInfo(data, callback){ //获取用户信息
	 Util.post(API.getUserInfo, data, function(res) {
				if(res.code == 200) {
					$('#userName').val(res.data.userName);
					localStorage.userName = res.data.userName; //将用户名缓存
					localStorage.cardNo =  res.data.cardNo; //将用户身份证号缓存
					localStorage.phone = res.data.phone; //缓存用户手机号
					if(res.data.havePayPwd == true){
						$('#title').text('身份验证');
						html = template.render("template_paywdBox", res);
						$("#paywdCont").html(html);
					}else{
						$('#title').text('设置提现密码');
						html = template.render("template_setwdBox", res);
						$("#paywdCont").html(html);
					}
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});		
	},
	getBankInfo(data, callback) {//根据银行卡号获取银行信息
		Util.post(API.getBankInfo, data, function(res) {
			if(res.code == 200) {
				if(callback)
					callback(res)
			} else {
				layer.open({content:res.message,skin: 'msg',time: 2});
			}
		});
	},
	bundQuickBankCard(data, callback) {//添加绑定银行卡
		Util.post(API.bundQuickBankCard, data, function(res) {
			if(res.code == 200) {
				if(callback)
					callback(res)
			} else {
				layer.open({content:res.message,skin: 'msg',time: 2});
			}
		});
	}
}

var Plan = { //计划模块
		getPlanDetail(data, callback){ //计划详情数据
			 Util.post(API.gateway, data, function(res) {
					if(res.code == 200) {
						res.data.capitalAmount = (res.data.capitalAmount/100).toFixed(2);
						res.data.totalAmount = (res.data.totalAmount/100).toFixed(2);
						res.data.poundageTotalAmt = (res.data.poundageTotalAmt/100).toFixed(2);
						res.data.firstDay = '';
						for(var i in res.data.planDetails){ //数据处理调整时间格式等
							var repay = 0;
							var consume = 0;
							for(var k in res.data.planDetails[i]){ //当天消费金额与还款金额总额
							   if(res.data.firstDay == ''){
								res.data.firstDay = res.data.planDetails[i][k].planDate;
								}
								res.data.planDetails[i][k].planDate = res.data.planDetails[i][k].planDate.split('-')[1]+'-'+res.data.planDetails[i][k].planDate.split('-')[2];
								if(res.data.planDetails[i][k].planType == 20){ //当日还款总额
									repay = repay+res.data.planDetails[i][k].planAmount;
								}
								if(res.data.planDetails[i][k].planType == 10){ //当日消费总额
									consume = consume+res.data.planDetails[i][k].planAmount;
								}
								res.data.planDetails[i][k].planAmount = (res.data.planDetails[i][k].planAmount/100).toFixed(2);
							}
							res.data.planDetails[i][0].repay = (repay/100).toFixed(2);
							res.data.planDetails[i][0].consume = (consume/100).toFixed(2);
						}
						html = template.render("template_plandetails", res);
						$('#plandetails').html(html);
						if(callback)
							callback(res)
					} else {
						layer.open({content: res.message ,skin: 'msg',time: 2});
					}
				});	
		},
		getPlanList(data, callback){ //获取用户计划列表
		 Util.post(API.gateway, data, function(res) {
			if(res.code == 200) {
				if(res.data.length != 0){
					for(var i in res.data){
					res.data[i].totalAmount = ((res.data[i].totalAmount+res.data[i].poundageTotalAmt)/100).toFixed(2); //账单总额格式化
					res.data[i].poundageTotalAmt = (res.data[i].poundageTotalAmt/100).toFixed(2); //手续费格式化
						res.data[i].startDate = res.data[i].startDate.split('-')[1]+'月'+res.data[i].startDate.split('-')[2]+'日'; //日期处理
					}
					html = template.render("template_plan", res);
					$('#planBox').html(html);
				}else{
					var html =' <div class="zanwulogo"><img src="src/images/zanwulogo.png"/><p>目前暂无数据</p></div>';
					$('#planBox').html(html);
				}
				if(callback)
					callback(res)
			} else {
				layer.open({content: res.message ,skin: 'msg',time: 2});
			}
		});
	},
}

var Pay = { //支付模块
	planPayment(data, callback){
		 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});			
	},
	checkPayPwd(data, callback){
		Util.post(API.checkPayPwd, data, function(res) {
				if(res.code == 200) {
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
		});	
	},
	modifyPayPwd(data, callback){ //修改支付密码
		Util.post(API.modifyPayPwd, data, function(res) {
				if(res.code == 200) {
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
		});	
	}
}

var VerifyCode = { //验证码模块
	getVerifyCode(data, callback){
		 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					layer.open({content:'验证码已发送',skin: 'msg',time: 2});
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
				
				/*-------获取验证码时倒计时--------*/
				var time = 60;
				$('.getems').css('background-color', '#f0f0f0');
				$('.getems').off('click');
				var timer = setInterval(function() {
					if(time == 1) {
						time = 60;
						$('.getems').text('重新发送');
						$('.getems').css('backgroundColor', '#fff');
						clearInterval(timer);
						//倒计时完成，初始化点击事件
						$('.getems').on('click', function() {
							if($(this).hasClass('getcode')){ //判断是否有getcode该样式，若有则该参数，判断为支付页面时的验证码
								var phone = $('#paybody').data('phone').toString();
									phone = phone.substr(0, 3)+'****'+phone.substr(7, 11);
							  	 VerifyCode.getVerifyCode({
							    	method: 'plan.sendsms',
							    	version: '1.0.2',
							    	userId: localStorage.rpUserId, 
							    	bundCardId: localStorage.bundcardid, //银行卡id
							    	cardType: localStorage.cardtype //卡类型，0=储蓄卡，1=信用卡
							    },function(res){
							    	layer.open({content:'验证码已发送至'+phone,skin: 'msg',time: 2});
							    });
							}else{
								 VerifyCode.getVerifyCode({
							    	method: 'plan.sendsms',
							    	version: '1.0.2',
							    	userId: localStorage.rpUserId, 
							    	bundCardId: sessionStorage.bankCardId, //银行卡id
							    	cardType: 1 //卡类型，0=储蓄卡，1=信用卡
							    },function(res){});
							} 
						});
					} else {
						time--;
						$('.getems').text(time + 's后重试');
					}
				}, 1000);
				/*---------------*/
			});		
	},
	planOpenQuick(data, callback){ //确认鉴权
	 Util.post(API.gateway, data, function(res) {
				if(res.code == 200) {
					console.log(res);
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
			});			
	},
	checkVerifyCode(data, callback){ //修改支付密码验证码
			 Util.post(API.checkVerifyCode, data, function(res) {
				if(res.code == 200) {
					if(callback)
						callback(res)
				} else {
					layer.open({content: res.message ,skin: 'msg',time: 2});
				}
				
				/*-------获取验证码时倒计时--------*/
				var time = 60;
				$('.getems').off('click');
				var timer = setInterval(function() {
					if(time == 1) {
						time = 60;
						$('.getems').text('重新发送');
						clearInterval(timer);
						//倒计时完成，初始化点击事件
						$('.getems').on('click', function() {
								VerifyCode.checkVerifyCode({
									phone: localStorage.phone, //用户手机号
									bizType: 4 //验证码类型
								},function(res){})
						});
					} else {
						time--;
						$('.getems').text(time + 's后重试');
					}
				}, 1000);
				/*---------------*/
			});	
	}
}

